#include "overlayer.h"

Overlayer::Overlayer(QObject *parent) :
    QThread(parent)
{
    map=new QMap<qint64, int>;
    model=new QFileSystemModel();
    count=0;
    processed=false;
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(onTimerTimeout()));
}

void Overlayer::run(){
    if (!timer->isActive()){
        processChanges("");
        timer->start(5000);
    }
}

void Overlayer::processChanges(QString txt){
    QString aux,str_value;
    QStringList list;
    QStringList list2;
    QString dir;
    //QRegExp rx("\\? .*");
    QRegExp rx_modfied("M .*");
    QRegExp rx_unknown("\\? .*");
    QRegExp rx_added("A .*");
    QRegExp rx_clear("C .*");

    qint64 id;
    list=txt.split("\n");

    changedFiles=0;
    for (int i=0; i< list.size();i++)
    {
        aux=list.at(i);
        if (rx_modfied.exactMatch(aux)){
            str_value=aux.replace(0,2,"");
            id=model->index(QDir::toNativeSeparators(basePath)+QDir::separator()+str_value).internalId();
            map->insert(id,ListViewDelegate::FileModified);
            list2=str_value.split(QDir::separator());
            for (int j=0; j< list2.size()-1;j++){
                if (j==0)
                    dir=list2.at(j);
                else
                    dir.append(QDir::separator()).append(list2.at(j));
                id=model->index(QDir::toNativeSeparators(basePath)+QDir::separator()+dir).internalId();
                map->insert(id,ListViewDelegate::FileModified);
            }
        }else if (rx_unknown.exactMatch(aux)){
            str_value=aux.replace(0,2,"");
            id=model->index(QDir::toNativeSeparators(basePath)+QDir::separator()+str_value).internalId();
            map->insert(id,ListViewDelegate::FileUnknown);
            list2=str_value.split(QDir::separator());
            for (int j=0; j< list2.size()-1;j++){
                if (j==0)
                    dir=list2.at(j);
                else
                    dir.append(QDir::separator()).append(list2.at(j));
                id=model->index(QDir::toNativeSeparators(basePath)+QDir::separator()+dir).internalId();
                map->insert(id,ListViewDelegate::FileModified);
             }
        }else if (rx_added.exactMatch(aux)){
            str_value=aux.replace(0,2,"");
            id=model->index(QDir::toNativeSeparators(basePath)+QDir::separator()+str_value).internalId();
            map->insert(id,ListViewDelegate::FileAdded);
            list2=str_value.split(QDir::separator());
            for (int j=0; j< list2.size()-1;j++){
                if (j==0)
                    dir=list2.at(j);
                else
                    dir.append(QDir::separator()).append(list2.at(j));
                id=model->index(QDir::toNativeSeparators(basePath)+QDir::separator()+dir).internalId();
                map->insert(id,ListViewDelegate::FileModified);
            }
        }else if (rx_clear.exactMatch(aux)){
            str_value=aux.replace(0,2,"");
            id=model->index(QDir::toNativeSeparators(basePath)+QDir::separator()+str_value).internalId();
            //map->remove(id);
            map->insert(id,ListViewDelegate::FileClear);
        }

        changedFiles++;
    }
    //qDebug() << "changed files" << changedFiles;
    processed=true;
    emit mapReady(map);
}

void Overlayer::setBasePath(QString path){
    basePath=path;
    count=0;
    emit commandRequired("ar-changes");
}

void Overlayer::setModel(QFileSystemModel *model){
    this->model=model;
}

void Overlayer::onTimerTimeout(){
    if (processed)
        processed=false;
    else if (changedFiles>0){
        paintClear();
        changedFiles=0;
    }
    //qDebug() << "Timer triggered";
    emit commandRequired("ar-changes");
}

void Overlayer::paintClear(){
    qint64 id;
    QDirIterator it(basePath, QDirIterator::Subdirectories);
    //qDebug() << "Painting clear path" << basePath;
    map->clear();
    while (it.hasNext()) {
        //qDebug() << it.filePath();
        it.next();
        id=model->index( it.filePath()).internalId();
        //map->remove(id);
        map->insert(id,ListViewDelegate::FileClear);
     }
    emit mapReady(map);
}
